# coding:utf-8
# author:frank
# date:2024/12/31
import pygame

clock = pygame.time.Clock()
pygame.init()

win_w=500
win_h=600
win = pygame.display.set_mode((win_w,win_h))
pygame.display.set_caption("标题模板")
win.fill('white')
pygame.display.flip()

right = pygame.image.load('files/left-1.png')
left = pygame.image.load('files/right-1.png')


left_rect = left.get_rect()
left_rect.x,left_rect.y = 0,200
right_rect = right.get_rect()
right_rect.x,right_rect.y = 400,200
# 获取两个图片的遮盖层对象
left_mask = pygame.mask.from_surface(left)
right_mask = pygame.mask.from_surface(right)

while True:
    clock.tick(30)
    win.fill('white')
    win.blit(right,(right_rect.x,right_rect.y))
    win.blit(left,(left_rect.x,left_rect.y))
    pygame.display.update()
    # overlap和colliderect的区别在于，overlap可以检测两个精灵的遮盖层是否重叠，而colliderect只能检测两个精灵的矩形区域是否重叠。
    # 遮盖层对象1.overlap(遮盖层对象2，(x,y)偏移量)
    if left_mask.overlap(right_mask, (right_rect.x-left_rect.x, right_rect.y-left_rect.y)):
        print('碰撞了')
    else:
        left_rect.x += 1
        right_rect.x -= 1
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
